package com.itheima.travel.web;

import com.itheima.travel.req.BaseRequest;
import com.itheima.travel.res.GetwayRespons;
import com.itheima.travel.service.GatewayService;
import com.itheima.travel.utils.EmptyUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

//后台网关
@Controller
@RequestMapping("/api-platform")//窄化路劲
@Log4j2
public class ApiPlatformController {
    @Autowired
    private GatewayService gatewayService;

    @RequestMapping(value = "/{service}/{method}")
    @ResponseBody
    //解决跨域问题
    @CrossOrigin
    public GetwayRespons apiKoan(@PathVariable("service") String service, @PathVariable("method") String method, MultipartFile multipartFile, @RequestBody BaseRequest baseRequest){
        //定义网关对象
        GetwayRespons getwayRespons=null;
        if(EmptyUtil.isNullOrEmpty(service)||EmptyUtil.isNullOrEmpty(method)) {
            //如果两个有一个为空，则就格式不对
            log.warn("api-loan请求不合法！---> 调用者[{}]--->调用方法[{}]");
            return GetwayRespons.builder().data(null).code("1001").msg("参数不合法").build();
        }else {
            log.info("api-loan请求：调用类[{}]--->方法[{}]--->请求参数[{}]", service, method, baseRequest.toString());
            getwayRespons = gatewayService.methods(method, baseRequest, multipartFile);
        }
        return getwayRespons;
        }
    }
